﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Bussiness;
using Bussiness.Service;
using CKEditor.NET;

namespace KiwiShopping.Addmin.BackEnd
{
    public partial class MG_Artical : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
            }

            if(X.IsAjaxRequest){
                string action = Request.Params.Get("Action");
                string id = Request.Params.Get("ID");
                if (action != null && id != null)
                {
                    if (action == "Delete") Delete(int.Parse(id));
                    if (action == "Edit") Edit(int.Parse(id));
                }

            }
        }

        public void loadData()
        {
            //load data selectbox danh muc cha
            DMCha_Store.DataSource = new Catelogy_Service().getCateloriesArticalParent();
            DMCha_Store.DataBind();

            //load data grdi danh muc con
            //DMCon_Store.DataSource = new Catelogy_Service().getAllCatelogiesArticalChild();
            //DMCon_Store.DataBind();

            tk_DMCon_Store.DataSource = new Catelogy_Service().getAllCatelogiesArticalChild();
            tk_DMCon_Store.DataBind();
        }

        protected void Save(object sender, DirectEventArgs e)
        {
            Appcontex app = (Appcontex)Session["app"];
            Article obj = new Article();
            obj.Title = dt_title.Text;
            obj.IsBanner = isBanner.Checked;
            if (Formater.isNull(dt_ID.Text) == false)
            {
                obj.ID = int.Parse(dt_ID.Text);
            }
            obj.SubImage = dt_image.Text;
            if (!Formater.isNull(DMCon.SelectedItem.Value))
            obj.IdCategories = int.Parse(DMCon.SelectedItem.Value);
            obj.SubContent = dt_gioithieu.Text;
            obj.Writer = app.TenDangNhap;
            obj.Date = DateTime.Now;
            //obj.Content = HttpUtility.HtmlEncode(dt_content2.Text);
            obj.Content = dt_content2.Text;
            new Artical_Service().save_Article(obj);
            MessageBox mgb = new MessageBox();
            mgb.Alert("", "Lưu thành công");
            mgb.Show();
        }

        protected void SelectParent(object sender, DirectEventArgs e)
        {
            
            DMCon_Store.DataSource = new Catelogy_Service().getAllCatelogiesArticalChild(int.Parse(DMCha.SelectedItem.Value));
            DMCon_Store.DataBind();
        }

        protected void Search(object sender, DirectEventArgs e)
        {
            Article obj = new Article();
            obj.Title = tk_title.Text;
            if(!Formater.isNull(tk_DMCon_Grid.SelectedItem.Value))
            obj.IdCategories = int.Parse(tk_DMCon_Grid.SelectedItem.Value);
            obj.Date = tk_Ngaybd.SelectedDate;
            obj.DateEnd = tk_Ngaykt.SelectedDate;

            Artical_Store.DataSource = new Artical_Service().search_Article(obj);
            Artical_Store.DataBind();
        }

        public void Edit(int id)
        {
            Article obj = new Artical_Service().getArticleByID(id);
            dt_ID.Text = obj.ID.ToString();
            dt_title.Text = obj.Title;
            isBanner.Checked = obj.IsBanner.Value;
            dt_image.Text = obj.SubImage;
            dt_gioithieu.Text = obj.SubContent;
            dt_content2.Text = obj.Content;
           

            string parentID = new Catelogy_Service().getCatelogyArtical(obj.IdCategories.Value).ParentId.ToString();
            DMCha.SelectedItem.Value = parentID;
            DMCon_Store.DataSource = new Catelogy_Service().getAllCatelogiesArticalChild(int.Parse(parentID));
            DMCon_Store.DataBind();

            DMCon.SelectedItem.Value = obj.IdCategories.ToString();

            TabPanel1.ActiveTabIndex = 0;
            X.Js.Call("setValueFCK");
           
        }

        public void Delete(int id)
        {
            new Artical_Service().delete_Article(id);
            MessageBox mgb = new MessageBox();
            mgb.Alert("", "Xóa thành công");
            mgb.Show();

            //TabPanel1.ActiveTabIndex = 1;
            Artical_Store.DataSource = new Artical_Service().getAll_Article();
            Artical_Store.DataBind();
        }
    }
}